package listeners;

import gui.MainWindow;
import gui.panel.ConnectionPanel;
import gui.panel.HomePanel;
import gui.panel.MainTabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Signals.Goodbye;

public class DisconnectButtonListener implements ActionListener {

	private HomePanel homePanel;
	
	public DisconnectButtonListener(HomePanel homePanel) {
		this.homePanel = homePanel;
	}
	
	/**
	 * This method does not work (blocks the program)
	 * 
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		this.homePanel.getNetworkInterface().getReceiveManager().setConnected(false);
		this.homePanel.getNetworkInterface().getSendManager().sendBroadcast(new Goodbye(this.homePanel.getNetworkInterface().getNickname()));
		this.homePanel.getNetworkInterface().closeSocket();
		this.homePanel.getNetworkInterface().stopReceiving();
		
		this.homePanel.getNetworkInterface().closeSocket();
		
		MainWindow.getInstance().getContentPane().removeAll();
		MainWindow.getInstance().getContentPane().add(new ConnectionPanel());
		MainWindow.getInstance().pack();
		
	}

	
	
}
